package com.eqdd.yiqidian.ui.yiqixue.add;

import android.Manifest;
import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.afollestad.materialdialogs.folderselector.FileChooserDialog;
import com.eqdd.nextinputs.StaticScheme;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.RecyclerViewCustom;
import com.eqdd.yiqidian.base.AppConstent;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.WriteBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditIconBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.RichEditorActivity;
import com.eqdd.yiqidian.ui.base.EditTextActivity;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.ui.select.SelectTwoDeepMultiTagActivity;
import com.eqdd.yiqidian.utils.PermissionTipUtil;
import com.eqdd.yiqidian.utils.PicUtil;
import com.eqdd.yiqidian.utils.SplitUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.io.File;
import java.util.List;

/**
 * @author吕志豪 .
 * @date 18-2-25  上午9:26.
 * Github :https://github.com/lvzhihao100
 * E-Mail：1030753080@qq.com
 * 简书 :http://www.jianshu.com/u/6e525b929aac
 */

public class AddLectureActivity extends SlimBaseMultiEditActivity implements FileChooserDialog.FileCallback {

    private RecyclerViewCustom dataBinding;
    private File coursewares;
    private Bundle bundle;
    private WriteBean writeBean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_recyclerview);
        initTopTitleBar(View.VISIBLE, "发布课程");
        initTopRightText("发布", v -> {
            if (checkParams()) {
                HttpParams params = new HttpParams();
                if (coursewares != null) {
                    params.put("coursewares", coursewares);
                }
                if (setParam(9) != null && !"".equals(setParam(9))) {
                    params.put("files", new File(setParam(9)));
                }
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.LEARN_ADD_LECTURE)
                        .params("userGuid", AppConstent.user.getGuid())
                        .params("courseType", setParam(1))
                        .params("courseTheme", setParam(0))
                        .params("courseTimes", setParam(3))
                        .params("courseMethod", " ")
                        .params("courseOutlint", writeBean.getHtml())
                        .params("courseObjecter", setParam(2))
                        .params("coursePrice", setParam(4))
                        .params("courseBackground", setParam(7))
                        .params("lectureName", AppConstent.user.getUsername())
                        .params("courseTarget", setParam(6))
                        .params(params)
                        .execute(new DialogCallBack<HttpResult>(AddLectureActivity.this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                bundle = new Bundle();
                bundle.putInt(Config.OPTION, 45);
                bundle.putInt(Config.MAX_NUM, 5);
                startActivityForResult(SelectTwoDeepMultiTagActivity.class, bundle, Config.SELECT_TWO_DEEP_MULTI_TAG);
                break;
            case 3:
                Intent intent = new Intent(this, EditTextActivity.class);
                intent.putExtra("onlyNumber", true);
                startActivityForResult(intent, 3);
                break;
            case 4:
                Intent intent1 = new Intent(this, EditTextActivity.class);
                intent1.putExtra("onlyNumber", true);
                startActivityForResult(intent1, 4);
                break;
            case 5:
                RxPermissions.getInstance(this).request(Manifest.permission.READ_EXTERNAL_STORAGE)
                        .subscribe(isGrant -> {
                            if (isGrant) {

                                new FileChooserDialog.Builder(this)
                                        .initialPath("/sdcard/Download")  // changes initial path, defaults to external storage directory
                                        .mimeType("image/*") // Optional MIME type filter
                                        .extensionsFilter(".png", ".jpg") // Optional extension filter, will override mimeType()
                                        .tag("optional-identifier")
                                        .goUpLabel("上一级") // custom go up label, default label is "..."
                                        .show(this);
                            } else {
                                PermissionTipUtil.tip(this, "存储");
                            }
                        });
                break;
            case 8:
                bundle = new Bundle();
                bundle.putString(Config.TITLE, "课程大纲");
                bundle.putSerializable(Config.BEAN, writeBean);
                startActivityForResult(RichEditorActivity.class, bundle, Config.RICH_EDITOR);
                break;
            case 9:
                PicUtil.single(this);
                break;
        }
    }

    @Override
    protected void setEditBeans() {
        isRead = false;
        isSave = false;
        datas.add(new SlimEditEnterBean("*课程主题", "", 0, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*课程类型", "", 1, true, "请选择").setNeed(true));
        datas.add(new SlimEditEnterBean("*授课对象", "", 2, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*课程时长(天)", "", 3, true, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("*课程价格(元/天)", "", 4, true, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true));
        datas.add(new SlimEditEnterBean("课程课件", "", 5, true, "请选择"));
        datas.add(new SlimEditEnterBean("*课程目标", "", 6, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*课程背景", "", 7, "请输入").setNeed(true));
        datas.add(new SlimEditEnterBean("*课程大纲", "", 8, true, "请输入"));
        datas.add(new SlimEditIconBean("*课程封面", "", 9, true).setIconClicked(false).setNeed(true));


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == PictureConfig.CHOOSE_REQUEST) {
            List<LocalMedia> localMedias = PictureSelector.obtainMultipleResult(data);
            slimAdapterEx.getDataItem(9).setContent(localMedias.get(0).getCompressPath());
            slimAdapterEx.notifyItemChanged(9);
        } else if (resultCode == Config.SUCCESS) {
            switch (requestCode) {
                case Config.SELECT_TWO_DEEP_MULTI_TAG:
                    List tags = data.getStringArrayListExtra(Config.STRING_LIST);
                    String combin = SplitUtil.combin(tags, ",");
                    slimAdapterEx.getDataItem(1).setContent(combin);
                    slimAdapterEx.notifyItemChanged(1);
                    break;
                case Config.RICH_EDITOR:
                    writeBean = (WriteBean) data.getSerializableExtra(Config.BEAN);
                    slimAdapterEx.getDataItem(8).setContent("已填写");
                    slimAdapterEx.notifyItemChanged(8);
                    break;

            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    @Override
    public void onFileSelection(@NonNull FileChooserDialog dialog, @NonNull File file) {
        coursewares = file;
        slimAdapterEx.getDataItem(5).setContent(file.getName());
        slimAdapterEx.notifyItemChanged(5);
    }

    @Override
    public void onFileChooserDismissed(@NonNull FileChooserDialog dialog) {

    }
}
